package cn.xaut.XAUT迎春杯预热;

import java.util.Arrays;
import java.util.Scanner;
import static java.lang.Math.*;

/**
 * J - 修井
 */
public class demoJ {

    // 求数组的中位数
    static long getMedian(long[] nums) {
        
        int n = nums.length;
        Arrays.sort(nums);
        return nums[n / 2];
    }
    
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        long[] a = new long[N];
        for (int i = 0; i < N; i++) 
            a[i] = input.nextLong();
        long address = getMedian(a);
        long res = 0;
        for (long x : a) 
            res += abs(x - address);
        System.out.println(res);
    }
}
